package com.se.config;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import javax.servlet.ServletContext;

import org.apache.commons.configuration.BaseConfiguration;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.util.Assert;
import org.springframework.web.context.ServletContextAware;

public class WebPreferencesFactoryBean implements FactoryBean, InitializingBean, DisposableBean, ServletContextAware, ApplicationContextAware {

	private static final Logger logger = Logger.getLogger(WebPreferencesFactoryBean.class);

	private ServletContext servletContext;

	private Preferences preferences;

	private String basePath = "/WEB-INF/config/preferences";

	private String filename = "configuration.xml";

	private ApplicationContext ctx;

	private File baseDirectory;

	private String encoding = "UTF-8";

	public void setServletContext(ServletContext paramServletContext) {
		this.servletContext = paramServletContext;
	}

	public void setApplicationContext(ApplicationContext paramApplicationContext) throws BeansException {
		this.ctx = paramApplicationContext;
	}

	public Object getObject() throws Exception {
		return this.preferences;
	}

	public Class getObjectType() {
		return ((this.preferences == null) ? Preferences.class : this.preferences.getClass());
	}

	public boolean isSingleton() {
		return true;
	}

	public void afterPropertiesSet() throws ConfigurationException {
		if (logger.isDebugEnabled()) logger.debug("Initializing Configuration for web application.");
		Assert.notNull(this.ctx, "ApplicationContext must not be null.");
		Assert.notNull(this.servletContext, "ServletContext must not be null.");
		String str = "";
		if (this.servletContext.getRealPath("/") != null) {
			this.baseDirectory = new File(this.servletContext.getRealPath("/"), this.basePath);
			str = this.servletContext.getRealPath("/");
		} else {
			this.baseDirectory = new File(this.basePath);
			str = this.basePath.substring(1, this.basePath.indexOf("WEB-INF"));
		}
		File localFile = new File(this.baseDirectory, this.filename);
		CommonsConfigurationPreferences localCommonsConfigurationPreferences = new CommonsConfigurationPreferences();
		XMLConfiguration localXMLConfiguration1 = new XMLConfiguration();
		localXMLConfiguration1.setEncoding(this.encoding);
		localXMLConfiguration1.setFile(localFile);
		localXMLConfiguration1.load();
		Object localObject1 = localXMLConfiguration1.getProperty("xml[@fileName]");
		ArrayList localArrayList = new ArrayList();
		if (localObject1.getClass().isAssignableFrom(String.class)) localArrayList.add(localObject1);
		else localArrayList.addAll((Collection) localObject1);
		Iterator localIterator = localArrayList.iterator();
		Object localObject2 = null;
		while (localIterator.hasNext()) {
			localObject2 = (String) localIterator.next();
			if (logger.isDebugEnabled()) logger.debug("Loading configuration xml file[" + ((String) localObject2) + "].");
			XMLConfiguration localXMLConfiguration2 = new XMLConfiguration();
			FileChangedReloadingStrategy localFileChangedReloadingStrategy = new FileChangedReloadingStrategy();
			localFileChangedReloadingStrategy.setConfiguration(localXMLConfiguration2);
			localXMLConfiguration2.setReloadingStrategy(localFileChangedReloadingStrategy);
			localXMLConfiguration2.setEncoding(this.encoding);
			localXMLConfiguration2.setFile(new File(this.baseDirectory, (String) localObject2));
			localXMLConfiguration2.load();
			localCommonsConfigurationPreferences.addConfiguration((String) localObject2, localXMLConfiguration2);
		}
		Object localObject3 = new BaseConfiguration();
		if (logger.isDebugEnabled()) logger.debug("Checking servlet context attributes and building virtual configuration object.");
		((Configuration) localObject3).setProperty("contextPath", str);
		((Configuration) localObject3).setProperty("web.root", str);
		if (logger.isDebugEnabled()) logger.debug("Setting up servlet context path[" + ((Configuration) localObject3).getString("contextPath") + "].");
		localCommonsConfigurationPreferences.setVirtualConfiguration((Configuration) localObject3);
		this.preferences = localCommonsConfigurationPreferences;
	}
	

	public void destroy() throws Exception {
		if (logger.isDebugEnabled()) logger.debug("Destroying Configuration for web application.");
		this.preferences = null;
	}

	public void setBasePath(String paramString) {
		this.basePath = paramString;
	}

	public void setFilename(String paramString) {
		this.filename = paramString;
	}

	public String getEncoding() {
		return this.encoding;
	}

	public void setEncoding(String paramString) {
		this.encoding = paramString;
	}
}